#!/bin/bash

IMAGE_NAME=${IMAGE_NAME:-ccr.ccs.tencentyun.com/q1tv/admin-web}
echo "IMAGE_NAME: ${IMAGE_NAME}"

IMAGE_TAG=${IMAGE_TAG:-latest}
echo "IMAGE_TAG: ${IMAGE_TAG}"

current_version=$(kubectl get deploy admin-web -n default -o=jsonpath='{$.spec.template.spec.containers[:1].image}')

if [ ! -z ${current_version} ] && [ "${current_version}" = "${IMAGE_NAME}:${IMAGE_TAG}" ]; then
  kubectl rollout restart deploy admin-web -n default
else
  cat ./yaml/admin-web.yaml.template | sed 's|{image-tag}|'$IMAGE_TAG'|g' > ./yaml/admin-web.yaml
  kubectl apply -f ./yaml/admin-web.yaml -n default
fi
